<?php
require_once 'lib/controllerWithoutGetOrPost.php';
require_once 'view/standard-noleftbar-top.php';

function pageNavLink($totalpages, $pagenumber, $currentpage, $currentsort) {
	if($pagenumber <= $totalpages) {
		if($currentpage != $pagenumber) {
	 		return "<a href=\"featuringhistory.php?sort=".$currentsort."&page=".$pagenumber."\">".$pagenumber."</a>\n\t";	
		} else {
			return "<strong>[ ".$pagenumber." ]</strong>\n\t";
		}
	}
}


function insertSearchPageTopNavigation($sortnametype, $sortdatetype, $currentsort, $totalpages, $currentpage) {

$startpage = $currentpage;
if($currentpage < 3) {
	$startpage = 3;
}

$page1 = $startpage-2;
$page2 = $startpage-1;
$page3 = $startpage;
$page4 = $startpage+1;
$page5 = $startpage+2;

echo "<li class=\"sortby\">";
echo "<a class=\"leftfloat\" title=\"Sort by Name\" href=\"featuringhistory.php?sort=name".$sortnametype."\">Sort by Name</a>"; 
echo "<a class=\"leftfloat\" title=\"Sort by Date\" href=\"featuringhistory.php?sort=date".$sortdatetype."\">Sort by Date</a>";
		
echo "<a href=\"featuringhistory.php?sort=".$currentsort."&page=".$totalpages."\">&lt;&lt;</a>\n\t";
echo pageNavLink($totalpages, $page1, $currentpage, $currentsort);
echo pageNavLink($totalpages, $page2, $currentpage, $currentsort);
echo pageNavLink($totalpages, $page3, $currentpage, $currentsort);
echo pageNavLink($totalpages, $page4, $currentpage, $currentsort);
echo pageNavLink($totalpages, $page5, $currentpage, $currentsort);
echo "...\n\t";
echo "<a href=\"featuringhistory.php?sort=".$currentsort."&page=".$totalpages."\">".$totalpages."</a>\n\t";
echo "<a href=\"featuringhistory.php?sort=".$currentsort."&page=".$totalpages."\">&gt;&gt;</a>\n\t";
echo "</li>";
						
	
}

function insertSearchPageBottomNavigation($sortnametype, $sortdatetype, $currentsort, $totalpages, $currentpage) {

$startpage = $currentpage;
if($currentpage < 3) {
	$startpage = 3;
}

$page1 = $startpage-2;
$page2 = $startpage-1;
$page3 = $startpage;
$page4 = $startpage+1;
$page5 = $startpage+2;

echo "<li class=\"sortby\">";
echo "<a class=\"leftfloat\" title=\"Sort by Name\" href=\"featuringhistory.php?sort=name".$sortnametype."\">Sort by Name</a>"; 
echo "<a class=\"leftfloat\" title=\"Sort by Date\" href=\"featuringhistory.php?sort=date".$sortdatetype."\">Sort by Date</a>";
	

echo "<a href=\"featuringhistory.php?sort=".$currentsort."&page=1\">&lt;&lt;</a>\n\t";
echo pageNavLink($totalpages, $page1, $currentpage, $currentsort);
echo pageNavLink($totalpages, $page2, $currentpage, $currentsort);
echo pageNavLink($totalpages, $page3, $currentpage, $currentsort);
echo pageNavLink($totalpages, $page4, $currentpage, $currentsort);
echo pageNavLink($totalpages, $page5, $currentpage, $currentsort);
echo "...\n\t";
echo "<a href=\"featuringhistory.php?sort=".$currentsort."&page=".$totalpages."\">".$totalpages."</a>\n\t";
echo "<a href=\"featuringhistory.php?sort=".$currentsort."&page=".$totalpages."\">&gt;&gt;</a>\n\t";
echo "</li>";				
	
}


function commentNames($firstname, $lastname) {
	if(!empty($firstname) && !empty($lastname)) {
		return $lastname.", ".$firstname;
	} else if(!empty($firstname)) {
		return $firstname;
	} else if(!empty($lastname)) {
		return $lastname;
	} else {
		return "";
	}
}

function commentCountry($country) {
	/*if(!empty($city) && !empty($country)) {
		return $city.", ".$country.". ";
	} else if(!empty($city)) {
		return $city.". ";
	} else */	
	if(!empty($country)) {
		return $country;
	} else {
		return "";
	}
}

function clearFirstName($val) {
 if($val == "First Name") {
	return "Anonymous";
 } else {
	return $val;
 }
}

function clearSurname($val) { 
 if($val == "Last Name") {
	return "";
 } else {
	return $val;
 }
}

function clearTown($val) {
 if($val == "Town") {
	return "";
 } else {
	return $val;
 }
}

function clearCountry($val) {
 if($val == "not") {
	return 0;
 } else {
	return $val;
 }
}

function clipartDefault($val) {
	if($val == null || $val == "") {
		return 0;
	} else {
		return $val;
	}
}


			


function FirstNames($value) {
	if(isset($value) && $value != "") {
		return "<li><span>First Names: </span>".$value."</li>";
	}
}

function KnownAs($value) {
	if(isset($value) && $value != "") {
		return	"<li><span>Known as:</span>".$value."</li>";
	}
}

function LastName($value) {
	if(isset($value) && $value != "") {
		return "<li><span>Last Name:</span>".strtoupper($value)."</li>";
	}
}

function Nee($value) {
	if(isset($value) && $value != "") {
		return " nee ".$value;
	}
	return "";
}

function OtherNames($value, $nee) {
	if((isset($value) && $value != "") || (isset($nee) && $nee != "")) {
		return "<li><span>Other Names:</span>".strtoupper($value).Nee($nee)."</li>";
	}
}

function formatdatetime($date) {
	if($date == "0000-00-00 00:00:00") {
		return "";
	}   else {
		return date('d F Y', strtotime(str_replace("&", "&amp;", $date)));
	}
}

function BirthDate($value) {
	if(isset($value) && $value != "" && $value != "0000-00-00 00:00:00") {
		return "<li><span>Birth Date: </span>".formatdatetime($value)."</li>";
	}
}

function DeathDate($value) {
	if(isset($value) && $value != "" && $value != "0000-00-00 00:00:00") {
		return "<li><span>Death Date: </span>".formatdatetime($value)."</li>";
	}
}

function Age($value, $measure) {
	if(isset($value) && $value != "" && $value != "0") {
		return "<li><span>Age: </span>".$value.$measure."</li>";
	}
}

function Country($value) {
	if(isset($value) && $value != "") {
		return "<li><span>Country: </span>".$value."</li>";
	}
}

function CharityWeb($value, $http) {
	if(isset($http) && $http != "") {
		return "<a href=\"".$http."\">".$value."</a>";
	} else {
		return $value;
	}
}

function Charity($value, $http) {
	if(isset($value) && $value != "") {
		return "<li><span>Charity:</span> ".CharityWeb($value, $http)."</li>";
	}
}

?>
<script type="text/javascript">
function textCount() {
	elem = document.getElementById('comment');
	if (elem.value.length > 300) {
		elem.value = elem.value.substring(0, 300);
	} else {
		var counts = document.getElementById('count');
		counts.innerHTML = elem.value.length;
	}
}

function validate_email(field,alerttxt) {
	with (field) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) {
			alert("Please enter a valid email address.");
			return false;
		} else { return true; }
	}
}

function emailconfirm(field1, field2) {
	if(field1.value != field2.value && field1.value.length > 0 && field2.value.length > 0) {
		alert("Please double check that you have entered the same email address in both fields.")
	}
}


</script>
		<div id="contentarea">
			<ul id="fullbar">
				<?php 
						echo "<li class=\"header\"><h2>Previously Featured</h2></li>";
						
						$querycount = "select count(*) from FeaturingPerson";
						$resultcount = mysql_query($querycount, $link);
						
						if (!$resultcount) {
							$message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $querycount;
						    die($message);
						}
						
						$sort = "order by FeaturingPerson.DateAdded DESC, Person.Surname ASC";
						$sortnametype = "asc";
						$sortdatetype = "asc";
						if(isset($_GET['sort'])) {
							if($_GET['sort'] == "nameasc") {
								$sort = "order by Person.Surname ASC, Person.FirstNames ASC";
								$sortnametype = "desc";
								
							}
							if($_GET['sort'] == "namedesc") {
								$sort = "order by Person.Surname DESC, Person.FirstNames DESC";
								$sortnametype = "asc";
							}
							if($_GET['sort'] == "dateasc") {
								$sort = "order by FeaturingPerson.DateAdded ASC";
								$sortdatetype = "desc";
							}
							if($_GET['sort'] == "datedesc") {
								$sort = "order by FeaturingPerson.DateAdded DESC";
								$sortdatetype = "asc";
							}
						}
						
						
						$query = "SELECT `FeaturingPerson`.Blurb, `FeaturingPerson`.PersonID, `FeaturingPerson`.Position, FeaturingPerson.DateAdded, Person.Surname, Person.FirstNames FROM `FeaturingPerson` left join `Person` on `FeaturingPerson`.PersonID = Person.ID ".$sort." LIMIT ";

										
						$current = 0;


						$resultcount = mysql_result($resultcount, 0);

						$totalpages = 1;
						$resultsperpage = 50;

						$totalpages = ceil($resultcount/$resultsperpage);

						if($totalpages < 1) {
							$totalpages = 1;
						}

						$from = 0;
						$to = $resultsperpage;
						$currentpage = 1;

						if(isset($_GET['page'])) {
							$currentpage = $_GET['page'];
							$to = $_GET['page']*$resultsperpage;
							$from = $to-$resultsperpage;
	
							$query = $query.$from.",".$resultsperpage;	

						} else {
							$query = $query."0,".$resultsperpage;
						}

						$pagenumber = $_GET['page'];


						if($_GET['page'] > 0) {
							if($_GET['page'] < $totalpages) {
								$pagenumber = $_GET['page']+1;
							}	
						} else {
							$pagenumber = $_GET['page']+2;
						}
									
						
						
						//run main query to get all the listings
						$result = mysql_query($query, $link);
						
						// Check result
						// This shows the actual query sent to MySQL, and the error. Useful for debugging.
						if (!$result) {
						    $message  = 'Invalid query: ' . mysql_error() . "\n";
							$message .= 'Whole query: ' . $query;
							die($message);
						}			
							
							
							
												
						echo "<li class=\"resultinfo\"><span>(".$resultcount.") listing results &nbsp; (50) results per page &nbsp; (".$totalpages.") pages</span></li>";
						
						$currentsort = "";
						
						if(isset($_GET['sort'])) { $currentsort = $_GET['sort']; }
						
						
						insertSearchPageTopNavigation($sortnametype, $sortdatetype, $currentsort, $totalpages, $currentpage);
echo <<< EOT
<li class="visitorlist">
    <ul>
        <li><span><b>Featured person</b></span><span class="date"><b>Featured date</b></span></li>

EOT;
while ($row = mysql_fetch_assoc($result)) {
    if ($row['DateAdded'] <> '0000-00-00 00:00:00') {
        echo "<li><span><a href=\"remembrance.php?personid=".$row['PersonID']."\">".commentNames($row['FirstNames'],$row['Surname'])."</a></span><span class=\"date\">".date('d M Y', strtotime(str_replace("&", "&amp;", $row['DateAdded'])))."</span></li>";
    }
}
echo "</ul></li>";			
						insertSearchPageBottomNavigation($sortnametype, $sortdatetype, $currentsort, $totalpages, $currentpage);
				?>
				
			</ul>
			
		</div>
		
		<?php
require_once 'view/standard-noads-bottom.php';
?>